package com.kun.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.index.entity.Blog;
import com.kun.index.entity.Inform;
import com.kun.index.mapper.InformMapper;
import com.kun.index.service.InformService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 通知表 服务实现类
 * </p>
 *
 * @author kunkun
 * @since 2024-03-28
 */
@Service
public class InformServiceImpl extends ServiceImpl<InformMapper, Inform> implements InformService {

    @Autowired
    private InformMapper informMapper;


    @Override
    public Map<String, Object> queryInforms(HashMap<String, Object> map) {
        HashMap<String, Object> resultMap = new HashMap<>();
        QueryWrapper<Inform> wrapper = new QueryWrapper<>();

        if (map.get("queryTitleLike")!=null){
            wrapper.like("title",map.get("queryTitleLike"));
        }
        if (map.get("orderByCreateTime")!=null){
            if (map.get("orderByCreateTime").equals("createTime_desc")){
                wrapper.orderByDesc("createTime");

            }
        }


        if (map.get("pageIndex")!=null&&map.get("pageSize")!=null){
            int pageIndex = (Integer) map.get("pageIndex");
            int pageSize = (Integer) map.get("pageSize");

            Page<Inform> page = new Page<>(pageIndex,pageSize);
            informMapper.selectPage(page, wrapper);
            long total =  page.getTotal();
            List<Inform> informs = page.getRecords();
            resultMap.put("totalInformsNum",total);
            resultMap.put("informs",informs);
        }else {
            List<Inform> informs = informMapper.selectList(wrapper);
            resultMap.put("informs",informs);
        }
        return resultMap;
    }

    @Override
    public Integer addInform(Inform inform) {
        informMapper.insert(inform);
        return inform.getId();
    }

    @Override
    public Inform queryInformById(Integer informId) {
        return informMapper.selectById(informId);
    }

    @Override
    public int updateInform(Inform inform) {
        return informMapper.updateById(inform);
    }

    @Override
    public int deleteInform(Inform inform) {
        return informMapper.deleteById(inform.getId());
    }

    @Override
    public int batchDeleteInform(List<Integer> ids) {

        return informMapper.deleteBatchIds(ids);
    }

    @Override
    public Integer addInformHitsNum(Integer id) {
        Inform inform = this.queryInformById(id);
        Integer num= inform.getHitsNum()+1;
        inform.setHitsNum(num);
        this.updateInform(inform);
        return num;
    }
}
